#!/usr/bin/perl
#diskman-lib.pl

use Data::Dumper;

do "../web-lib.pl";

&init_config();

%hwinfo=&hwinfo("diskman");

&foreign_require("mount", "mount-lib.pl");
&foreign_require("raid", "raid-lib.pl");
&foreign_require("fdisk", "fdisk-lib.pl");
&foreign_require("proc", "proc-lib.pl");

%access = get_module_acl();

sub hwinfo
{
	my ($found,%hwinfo_cache);

	$found=0;

    
#	if(-d '/proc/scsi/aic79xx')
        { $hwinfo_cache{'hotswap_support'}=1; }

	open(FD,"/hwinfo")||die;
	while(<FD>){
		if( $_ =~ /^#/ ) { next; }
		if( $_ =~ /^\[$_[0]\]/ ) { $found=1; next; }
		if( $found ) {
			if( $_ =~ /(\S+)=(\S+)/ ) { $hwinfo_cache{$1}=$2; }	
		}
	}
	close(FD);
	return %hwinfo_cache;
}

sub get_diskindex
{

	my %sdinfo;

	open(SFDISK,"sfdisk -l|")||return undef;
	while(<SFDISK>){
		if(/Disk (\/dev\/sd[a-z]):/){
                        $sdinfo{&scsi_id($1)}=$1;
                }
        }
        close(SFDISK);

	my (@dinfo,$scsiid,$device,$index);

	$index=0;

	open(DISKINFO,$config{'diskinfo'})||return undef;
	while(<DISKINFO>){
		s/\r|\n//g;
		s/#.*$//g;
		if (/(\S+)\s*=\s*(\S+)\s+(\S+)/) {
			$device = $1;
			$status = $2;
			$scsiid = $3;

			$index++; 

			$dinfo[$index-1]->{'name'}   = 'disk'.$index;
			$dinfo[$index-1]->{'status'} = $status;
			$dinfo[$index-1]->{'scsiid'} = $scsiid;

			if ($scsiid =~ /\d+,\d+,\d+,\d+/) { 
				$dinfo[$index-1]->{'device'} = $sdinfo{$scsiid};
			} else {
				$dinfo[$index-1]->{'device'} = $device;
			}
			
		}
	}
	close(DISKINFO);
	return @dinfo;
}

# diskname2rdev(\dinfo, diskname)
# return the dev path of diskname
sub diskname2rdev
{
	my ($df, $diskname)=@_;
	my $rdev;

	foreach $d (@{$df}) {
		if ($d->{'name'} eq $diskname) {
			$rdev=$d->{'device'};
			last;
		}
	}
	return $rdev;
}

#return a array of diskinfo
sub get_luninfo
{
        my($i, $line, $disk_num, $scsiid, $device, $status, $index, $type);
        my($diskid,$capacity, $raidid, $style);
        my(@sys_diskpart_info,$d,$found,$sdfound,$userpart,@parts,$p,@stat);
        $disk_num = $hwinfo{'diskn'};
        for ($i = 0; $i < $disk_num; $i++) {
                $diskinfo_cache[$i]->{'device'} = '';
                $diskinfo_cache[$i]->{'info'} = '';
                $diskinfo_cache[$i]->{'capacity'} = '';
                $diskinfo_cache[$i]->{'vendor'} = '';
                $diskinfo_cache[$i]->{'model'} = '';
        }
        $index = -1;

        #get the status of disks from diskinfo(file)
        #open(DISKINFO, '/c0');
        open(DISKINFO, "/proc/scsi/scsi");
	@scsi=<DISKINFO>;
	close(DISKINFO);
        foreach $line (@scsi) {
	$index++;
        $line =~ s/\r|\n//g;
           $line =~ s/#.*$//g;
           if ($line =~ /Host:\s+scsi\d+\s+(Channel:.*)/){

						
              	$info = $1;
              	$capacity = $1;
		$scsi[$index+1] =~ /Vendor:\s+(\S+)\s+Model:\s+(\S+)\s+/;
              	$vendor = $1;
              	$model = $2;
		$scsi[$index+2] =~ /Type:\s+(\S+)\s+/;
		$type=$1;
		if($type =~ /Direct-Access/){
			$device = get_device($info);	
		}
		else
		{
			$device = "";
		}

           }else{
              	next;
           }
           $diskinfo_cache[$index]->{'device'} = $device;
           $diskinfo_cache[$index]->{'info'} = $info;
           $diskinfo_cache[$index]->{'capacity'} = $raidid;
           $diskinfo_cache[$index]->{'vendor'} = $vendor;
           $diskinfo_cache[$index]->{'model'} = $model;
        }
        #close(DISKINFO);
        return @diskinfo_cache;
}
sub get_device
{
	$info = $_[0];
	$info =~ /Lun:\s+(\S+)/;
	$lunid = int($1) + 1;
	return $lunid;	
}
sub get_disk_sizeM
{
	my ($d, $size);

	$d = $_[0];

	$size = $d->{'heads'} *	$d->{'sectors'} *
		($d->{'part_end'} - $d->{'part_start'}) / 2;
	$size = int($size/1024);
}



sub scsi_id
{
	$line=`scsi_info $_[0] 2>/dev/null`;
	if($line =~ /SCSI_ID="(\d+,\d+,\d+,\d+)"/) {
		return $1;
	}
	return undef;
}

sub get_luninfo
{
	open(SD, "/sbin/fdisk -l /dev/sd* |");
	@sd=<SD>;
	close(SD);
	$index = 0;
	foreach $s (@sd) {
		if($s =~ /Disk\s+(\/dev\/sd\S):\s(\S+)\s+GB,/){
			$dev=$1;
			$devsize=$2;
			$id=`/sbin/scsi_info $dev |grep SCSI_ID`;
			$id =~ /\"(\S+)\"/;
			$devid = $1;
			$model = `/sbin/scsi_info $dev |grep MODEL`;
			$model =~ /\"(.*)\"/;
			$devmodel=$1;
			$devcache[$index]->{'device'} = $dev;
			$devcache[$index]->{'capacity'} = $devsize;
			$devcache[$index]->{'info'} = $devid;
			$devcache[$index]->{'model'} = $devmodel;
			$index++;	
		}
		else{
			next;
		}
	}
	return @devcache;
}

1;
